﻿Imports System.Threading

' 因為用ACER筆電直接撥音效檔會卡住，所以獨立出DLL給MobileCenter參考
Public Class clsSndSnd
#Region " IDisposable Support "
    Implements IDisposable
    Private disposedValue As Boolean = False        ' 偵測多餘的呼叫
    ' IDisposable
    Protected Overridable Sub Dispose(ByVal disposing As Boolean)
        If Not Me.disposedValue Then
            If disposing Then
                SndStop()
            End If
            ' TODO: 釋放共用的 Unmanaged 資源
        End If
        Me.disposedValue = True
    End Sub
    ' 由 Visual Basic 新增此程式碼以正確實作可處置的模式。
    Public Sub Dispose() Implements IDisposable.Dispose
        ' 請勿變更此程式碼。在以上的 Dispose 置入清除程式碼 (ByVal 視為布林值處置)。
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub
#End Region

    ' 使用 WMI 取得音效卡內容   
    Private Function GetSoundCardInfo() As List(Of String)
        Dim rtnVal As New List(Of String)
        Dim colSoundCards As Object
        Dim objSoundCard As Object

        ' WMI (Windows Management Instrumentation ) 
        ' GetObject("winmgmts:") 使用GetObject 函數 取得 WMI 物件 
        ' 使用ExecQuery方法執行 Query查詢 
        ' Select * from Win32_SoundDevice  查詢音效裝置 
        colSoundCards = GetObject("winmgmts:").ExecQuery( _
                "Select * from Win32_SoundDevice")

        ' For Each / Next 迴圈列舉音效裝置及相關資訊 
        For Each objSoundCard In colSoundCards
            'MsgBox(objSoundCard.SystemName)     ' 系統名稱 
            'MsgBox(objSoundCard.Manufacturer)   ' 製造業者 , 廠商 
            'MsgBox(objSoundCard.ProductName)    ' 產品名稱 
            rtnVal.Add(objSoundCard.ProductName)
        Next
        Return rtnVal
    End Function
    Private mySoundCardInfo As List(Of String) = Nothing

    Private _defAlarmFile As String = ""

    Public Sub SndSnd(Optional ByVal UserWave As String = "")
        If (mySoundCardInfo.Count > 0) Then
            Dim SelWave As String = Me._defAlarmFile
            If UserWave <> "" AndAlso My.Computer.FileSystem.FileExists(UserWave) Then
                SelWave = UserWave
            End If

            If My.Computer.FileSystem.FileExists(SelWave) Then
                My.Computer.Audio.Play(SelWave, AudioPlayMode.BackgroundLoop)
            End If
        End If
    End Sub

    Public Sub SndStop()
        My.Computer.Audio.Stop()
    End Sub

    Public Sub New(ByVal defAlarmFile As String)
        Me._defAlarmFile = defAlarmFile
        mySoundCardInfo = GetSoundCardInfo()
    End Sub
End Class
